//    Copyright (C) Kherty.  All rights reserved.
using OpenLS.Core.Serialization;
using OpenLS.Drawing.Internals;

namespace OpenLS.Drawing
{
    internal class TableCellProperties : IOfficeXmlSerializable
    {
        internal const string xmlName = "tcPr";
        private TextAnchoringType anchor;
        private bool anchorCtr;
        private Cell3D cell3D;
        private OfficeArtExtensionList extLst;
        private FillProperties fillProperties;
        private TextHorzOverflowType horzOverflow;
        private LineProperties lnB;
        private LineProperties lnBlToTr;
        private LineProperties lnL;
        private LineProperties lnR;
        private LineProperties lnT;
        private LineProperties lnTlToBr;
        private double marB;
        private double marL;
        private double marR;
        private TextVerticalType vert;
        private double marT;

        #region IOfficeXmlSerializable Members

        public void ReadXml(ReadContext context)
        {
            using (ReadContext c = context.Read(xmlName))
            {
                marL = EmuConverter.FromEmu(c.GetOptionalInteger("marL", 91440));
                marR = EmuConverter.FromEmu(c.GetOptionalInteger("marR", 91440));
                marB = EmuConverter.FromEmu(c.GetOptionalInteger("marB", 45720));
                marT = EmuConverter.FromEmu(c.GetOptionalInteger("marT", 45720));
                vert = c.GetOptionalEnum("vert", TextVerticalType.Horizontal);
                anchor = c.GetOptionalEnum("anchor", TextAnchoringType.Top);
                anchorCtr = c.GetOptionalBoolean("anchorCtr", false);
                horzOverflow = c.GetOptionalEnum("horzOverflow", TextHorzOverflowType.Clip);
                lnL = c.ReadOptionalElementAs<LineProperties>("lnL");
                lnR = c.ReadOptionalElementAs<LineProperties>("lnR");
                lnT = c.ReadOptionalElementAs<LineProperties>("lnT");
                lnB = c.ReadOptionalElementAs<LineProperties>("lnB");
                lnTlToBr = c.ReadOptionalElementAs<LineProperties>("lnTlToBr");
                lnBlToTr = c.ReadOptionalElementAs<LineProperties>("lnBlToTr");
                cell3D = c.ReadOptionalElement<Cell3D>("cell3D");
                fillProperties = FillProperties.ReadFillProperties(c);
                extLst = c.ReadOptionalElement<OfficeArtExtensionList>("extLst");
            }
        }

        /// <summary>
        /// Convert an object into its XML representation
        /// </summary>
        /// <param name="context"></param>
        public void WriteXml(WriteContext context)
        {
            using (WriteContext c = context.Write(xmlName))
            {
                c.SetOptionalInteger64("marL", EmuConverter.ToEmu(marL), 91440);
                c.SetOptionalInteger64("marR", EmuConverter.ToEmu(marR), 91440);
                c.SetOptionalInteger64("marB", EmuConverter.ToEmu(marB), 45720);
                c.SetOptionalInteger64("marT", EmuConverter.ToEmu(marT), 45720);
                c.SetOptionalEnum("vert", vert, TextVerticalType.Horizontal);
                c.SetOptionalEnum("anchor", anchor, TextAnchoringType.Top);
                c.SetOptionalBoolean("anchorCtr", anchorCtr, false);
                c.SetOptionalEnum("horzOverflow", horzOverflow, TextHorzOverflowType.Clip);
                c.WriteOptionalElementAs(lnL, "lnL");
                c.WriteOptionalElementAs(lnR, "lnR");
                c.WriteOptionalElementAs(lnT, "lnT");
                c.WriteOptionalElementAs(lnB, "lnB");
                c.WriteOptionalElementAs(lnTlToBr, "lnTlToBr");
                c.WriteOptionalElementAs(lnBlToTr, "lnBlToTr");
                c.WriteOptionalElement(cell3D);
                c.WriteOptionalElement(fillProperties);
                c.WriteOptionalElement(extLst);
            }
        }

        #endregion
    }
}